In [1]:
#simple rounding
round(1.23, 1)
Out[1]:
In [2]:
round(-1.27, 1)
Out[2]:
In [5]:
#rounding at .5 goes to the even number
round(1.5,0)
Out[5]:
In [6]:
round(2.5,0)
Out[6]:
In [7]:
#rounding negative numbers rounds out the tens,hundreds...
a= 1627731
round(a, -1)
Out[7]:
In [8]:
round(a, -2)
Out[8]:
Use decimal for floating points that handle high precision base-10 numbers better
In [9]:
# example of the problem
a = 4.2
b = 2.1
a + b
Out[9]:
In [10]:
(a + b) == 6.3
Out[10]:
In [11]:
# with Decimal
from decimal import Decimal
# Decimal is constructed with strings!
a = Decimal('4.2')
b = Decimal('2.1')
a + b
Out[11]:
In [12]:
(a + b) == Decimal('6.3')
Out[12]:
In [14]:
# to change precision use a scoped local context
from decimal import localcontext
a = Decimal('1.3')
b = Decimal('1.7')
print(a / b) #0.7647058823529411764705882353
with localcontext() as ctx:
ctx.prec = 3
print(a / b)
In [22]:
import math
a = float('inf')
b = float('-inf')
c = float('nan')
a
Out[22]:
In [16]:
b
Out[16]:
In [17]:
c
Out[17]:
In [18]:
a+3
Out[18]:
In [19]:
3+b
Out[19]:
In [20]:
36/c
Out[20]:
In [25]:
math.isinf(b)
Out[25]:
In [26]:
math.isnan(c)
Out[26]:
In [45]:
# it is traditional to import numpy as np
import numpy as np
In [27]:
# python lists
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
x * 2
Out[27]:
In [28]:
# numpy array is a perf-optimized math-centric array
ax = np.array([1, 2, 3, 4])
ay = np.array([5, 6, 7, 8])
ax * 2
Out[28]:
In [29]:
ax + 10
Out[29]:
In [30]:
ax + ay
Out[30]:
In [31]:
ax * ay
Out[31]:
In [32]:
# numpy provides some universal math functions that work on arrays
np.sqrt(ax)
Out[32]:
In [33]:
np.cos(ax)
Out[33]:
In [34]:
#numpy arrays are stored like c arrays (contiguous blocks of same-sized memory) and so very large arrays can be used
grid = np.zeros(shape=(10000,10000), dtype=float)
grid
Out[34]:
In [36]:
grid += 10
grid
Out[36]:
In [37]:
np.sin(grid)
Out[37]:
multidimensional arrays
In [38]:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a
Out[38]:
In [39]:
# Select row 1
a[1]
Out[39]:
In [40]:
# Select column 1
a[:,1]
Out[40]:
In [41]:
# Select a subregion and change it
a[1:3, 1:3]
Out[41]:
In [42]:
a[1:3, 1:3] += 10
a
Out[42]:
In [43]:
# Broadcast a row vector across an operation on all rows
a + [100, 101, 102, 103]
Out[43]:
In [44]:
# Conditional assignment on an array
np.where(a < 10, a, 10) # syntax is where( condition, if_true, if_false)
Out[44]:
In [58]:
import random
values = [1, 2, 3, 4, 5, 6]
In [59]:
# seed the random function
random.seed() # Seed based on system time or os.urandom()
random.seed(12345) # Seed based on integer given
In [60]:
# pick a random item out of a sequence
random.choice(values)
Out[60]:
In [61]:
# take a sampling of N items where selected items are removed from further consideration
random.sample(values, 5)
Out[61]:
In [62]:
# shuffle items in a sequence in place
random.shuffle(values)
values
Out[62]:
In [63]:
# produce a random integer between 0 and 10 inclusive
random.randint(0,10)
Out[63]:
In [64]:
# produce a random floating point number between zero and one
random.random()
Out[64]: